/**成就系统管理类 */
class achievementMag extends eui.Component {
	/**关闭按钮 */
	private closeBtn: eui.Button;
	/**成就勋章显示容器 */
	private achievementVessel: eui.Group;
	/**勋章类 */
	private medalVessel: achievementMedal[] = [];
	public constructor() {
		super();
		this.skinName = "resource/assets/skins/achievement.exml";
		this.once(egret.Event.ADDED_TO_STAGE, this.init, this);
		DataProl.medal_main = this;
		st_config.setScreenAuto(this);
	}
	/**成就系统界面初始化 */
	private init() {
		this.closeBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.closeMain, this);
		this.loadMedal();

	}
	/**关闭界面 */
	private closeMain() {
		this.parent.removeChild(this);
		SoundManager.getInstance().playEffectSound("click_mp3");
	}

	/**改变用户皮肤状态 */
	public changeUserSkin(shopId, shopType, states) {
		this.screenMeg(shopId, shopType, states);
		let skinStates = [];
		for (let i = 0; i < GlobalData.userShopData.length; i++) {
			var skinChangeData1 = {
				accountId: DataProl.account,
				shopId: parseInt(GlobalData.userShopData[i].shopId),
				states: parseInt(GlobalData.userShopData[i].states)
			};
			skinStates.push(skinChangeData1);
		}
		//发送测试代码
		MiniNet.post(DataProl.shopaddr, '/setUserSkinStates', 'data=' + JSON.stringify(skinStates), this, this.setUserSkinCallBack);
	}
	/**筛选信息 */
	private screenMeg(shopId, shopType, states) {
		switch (states) {
			case 0:
				for (let i = 0; i < GlobalData.userShopData.length; i++) {
					if (parseInt(GlobalData.userShopData[i].shopId) == shopId) {
						GlobalData.userShopData[i].states = "1";
					}
				}
				break;
			case 1:
				for (let i = 0; i < GlobalData.userShopData.length; i++) {
					if (parseInt(GlobalData.userShopData[i].shopId) == shopId) {
						GlobalData.userShopData[i].states = "2";
					}
				}
				break;
			case 2:
				for (let i = 0; i < GlobalData.userShopData.length; i++) {
					if (parseInt(GlobalData.userShopData[i].shopId) == shopId) {
						GlobalData.userShopData[i].states = "3";
					}
					if (GlobalData.userShopData[i].shopType == shopType && GlobalData.userShopData[i].states == "3" && parseInt(GlobalData.userShopData[i].shopId) != shopId) {
						GlobalData.userShopData[i].states = "2"
					}
				}
				break;
			case 3:
				for (let i = 0; i < GlobalData.userShopData.length; i++) {
					if (parseInt(GlobalData.userShopData[i].shopId) == shopId) {
						GlobalData.userShopData[i].states = "2";
					}
				}
				break;
			default:
				break;
		}
	}
	/**更改皮肤状态成功回调 */
	private setUserSkinCallBack(usrRecorData) {
		var shopsData = JSON.parse(usrRecorData.target.response);
		if (shopsData.shopData == true) {
			this.achievementVessel.removeChildren();
			this.loadMedal();
			if (DataProl.login_main_handle != null) {
				DataProl.login_main_handle.saveUserSkin(GlobalData.userShopData);
			}

			console.log("更改皮肤成功" + shopsData);
		}
		else {
			console.log("更改皮肤失败" + shopsData);
		}
	}


	/**加载勋章 */
	private loadMedal() {
		let MedalNumber = 1;
		for (let i = 0; i < GlobalData.userShopData.length; i++) {
			let Medal = new achievementMedal(GlobalData.userShopData[i]);
			this.achievementVessel.addChild(Medal);
			this.medalVessel.push(Medal);
			MedalNumber++;

		}
		/**部署成就容器 */
		var tLayout: eui.TileLayout = new eui.TileLayout();
		tLayout.paddingTop = 110;
		tLayout.paddingBottom = 10;
		tLayout.paddingLeft = 100;
		tLayout.paddingRight = 10;
		tLayout.columnAlign = eui.ColumnAlign.JUSTIFY_USING_GAP;
		tLayout.rowAlign = eui.RowAlign.JUSTIFY_USING_GAP;
		tLayout.horizontalGap = 70;
		tLayout.verticalGap = 80;
		this.achievementVessel.layout = tLayout;
	}



}